package com.jxstyle.config;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.alibaba.fastjson.JSON;
import com.jxstyle.core.exception.BizException;
import com.jxstyle.core.exception.BizExceptionEnum;
import com.jxstyle.core.response.ErrorResponse;
import com.jxstyle.core.response.Response;
import com.jxstyle.core.util.ThreadLocalUtils;

@ControllerAdvice(basePackages = "com.jxstyle")
public class ResponseAdvice implements ResponseBodyAdvice<Object> {

	@Override
	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
		return true;
	}

	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {
		if (body instanceof Response) {
			return body;
		}
		Response<Object> response2 = new Response<>(body);
		if (body instanceof String) {
			return JSON.toJSONString(response2);
		}
		return response2;
	}

	@ExceptionHandler(BizException.class)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public ErrorResponse handleBizException(HttpServletRequest request, BizException ex) {
		ex.printStackTrace();
		return new ErrorResponse(ex.getCode(), ex.getMessage());
	}

	@ExceptionHandler(AccessDeniedException.class)
	@ResponseStatus(HttpStatus.OK)
	public Object handleAccessDeniedException(HttpServletRequest request, HttpServletResponse response,
			AccessDeniedException ex) {
		ex.printStackTrace();
		if (ThreadLocalUtils.getIsAjax()) {
			response.setContentType("application/json;charset=UTF-8");
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.print(JSON.toJSONString(new ErrorResponse(BizExceptionEnum.NO_AUTH)));
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (writer != null) {
					writer.flush();
					writer.close();
				}
			}
			return new ErrorResponse(BizExceptionEnum.NO_AUTH);
		}
		return new ModelAndView("/noauth");
	}

	@ExceptionHandler(RuntimeException.class)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public Object handleRuntimeException(HttpServletRequest request, HttpServletResponse response,
			RuntimeException ex) {
		ex.printStackTrace();
		return new ErrorResponse(BizExceptionEnum.SERVER_ERROR);
	}

	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public Object handleException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
		ex.printStackTrace();
		return new ErrorResponse(BizExceptionEnum.SERVER_ERROR);
	}
}
